package com.example.controller;

import com.example.common.HttpStatusConstants;
import com.example.common.R;
import com.example.model.req.AddOrderReq;
import com.example.model.resp.ListOrderResp;
import com.example.service.OrderService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@ResponseBody
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/add")
    public R<String> addOrder(@RequestBody AddOrderReq addorderReq) {
        return orderService.addOrder(addorderReq, request);
    }

    // 查看用户的所有订单
    @GetMapping("/list")
    public R<List<ListOrderResp>> getListOrderById() {
        return orderService.getListOrderByUserId(request);
    }

    // 结束订单
    @PostMapping("/complete")
    public R<String> completeOrder(@RequestParam("orderId") Integer orderId) {
        if (orderId == null) {
            return R.error(HttpStatusConstants.SERVICE_UNAVAILABLE, "");
        }
        return orderService.completeOrder(orderId);
    }

    // 退款申请
    @PostMapping("/reback/{orderId}")
    public R<String> orderReback(@PathVariable("orderId") Integer orderId) {
        if (orderId == null) {
            return R.error(HttpStatusConstants.SERVICE_UNAVAILABLE, "");
        }
        return orderService.orderReback(orderId, request);
    }

    @GetMapping("/pendingOrder")
    public R<List<ListOrderResp>> pendingOrderList() {
        return orderService.pendingOrderList(request);
    }

    @GetMapping("/progressOrder")
    public R<List<ListOrderResp>> progressOrderList() {
        return orderService.progressOrderList(request);
    }

    @GetMapping("/employee/orderList")
    public R<List<ListOrderResp>> employeeOrderList() {
        return orderService.employeeOrderList(request);
    }

    @PostMapping("/employee/progressOrder")
    public R<String> progressOrder(@RequestParam("orderId") Integer orderId) {
        return orderService.progressOrder(orderId);
    }

    @GetMapping("/employee/orderListByDate")
    public R<List<ListOrderResp>> searchOrderListByDate(@RequestParam("startDate") String startDate,
            @RequestParam("endDate") String endDate) {
        return orderService.searchOrderListByDate(startDate, endDate, request);
    }

}
